package com.flextribe.net
{
	import flash.utils.flash_proxy;
	
	use namespace flash_proxy;
	
	public dynamic class LocalStoreService extends FlexTribeService
	{
		private var _destination:String;
		private var _store:LocalStore;
		
		public function LocalStoreService(destination:String=""):void
		{
			_store = new LocalStore(destination);
		}
		
		public function get store():LocalStore{
			return _store;
		}
		
		public function get destination():String{
			return _destination;
		}
		
		public function set destination(value:String):void{
			_destination = value;
			if(_destination != null && _destination != ""){
				if(LocalStore.validateName(_destination)){
					_store = null;
					_store = new LocalStore(_destination);
				} else {
					throw new Error("Invalid characters in destination name");
				}
			}
		}
		
		public function commitProperties():Boolean{
			return _store.commitProperties();
		}
		
		public function get autoCommit():Boolean{
			return _store.autoCommit;
		}
		
		public function set autoCommit(value:Boolean):void{
			_store.autoCommit = value;
		}
		
		override flash_proxy function callProperty(name:*, ... args:Array):*{
	        var r:* = _store[name]; 
	        var i:int = 0;
	        while(i < args.length){
	        	r = r[args[i]];
	        	i++;
	        }
	        _store.soModified = true;
	        return r;
	    }
	    override flash_proxy function hasProperty(name:*):Boolean {
	        return _store.hasProperty(name);
	    }
		
		override flash_proxy function getProperty(name:*):* {
	        return _store[name];
	    }
		
		override flash_proxy function setProperty(name:*, value:*):void {
	        _store[name] = value;
	    }
		
	}
}